package generate;

import lombok.Data;
import lombok.SneakyThrows;
import org.junit.Test;

import java.io.*;
import java.util.*;

/**
 * @author xhh
 * @describe 描述
 * @date 2022/8/4 10:54
 * @ClassName TT1
 */
public class TT1 {
    @Test
    public void test1() {
        int b1=0,b2=1;
        int r=0,t;
        for (int i = 0; i < 20; i++) {
            System.out.println("i["+i+"]:"+b1);
            t=b1;
            b1=b2+b1;
            b2=t;
        }
    }
    @Test
    public void test2() {
        String strA="123abcedfadbf abc321";
        Map<Character,Integer> map=new HashMap<>();
        char[] chars = strA.toCharArray();
        for (char aChar : chars) {
            if(map.containsKey(aChar)){
                map.put(aChar,map.get(aChar)+1);
            }else{
                map.put(aChar,1);
            }
        }
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            if(entry.getValue()>1){
                System.out.println(entry.getKey());
            }
        }
    }
    @Test
    public void test3(){
        String str = "Split sentence when you have : , questions marks ? or email @.";
        String[] split = str.split("\\W+");
        for (String s : split) {
            System.out.println(s);
        }


    }
    @Test
    public void test4(){
        int l=5,h=6,num=0;
        for (int i = 1; i <= l; i++) {
            List list1=new ArrayList();
            for(int j = i; j <= l; j++){
                list1.add(j);
                //System.out.println("长:"+list1);
                for (int ii = 1; ii <=h; ii++) {
                    List list2=new ArrayList();
                    for (int jj = ii; jj <= h; jj++) {
                        list2.add(jj);
                        if(list2.size()==list1.size()){
                            num++;
                            System.out.println("长:"+list1+","+"宽:"+list2);
                        }
                    }
                }
            }
        }
        System.out.println("一共有"+num+"个");
    }
    @Test
    public void test5(){
        List<Integer> list=new ArrayList<>();
        list.add(4);
        list.add(5);
        list.add(1);
        Collections.sort(list);
        list.forEach(e-> System.out.println(e));

    }
    @Test
    public void test6(){
        //用compareble comparetor排序list
        List<User> list=new ArrayList<>();
        list.add(new User("a",12));
        list.add(new User("b",11));
        list.add(new User("c",14));
        list.add(new User("d",10));
        Comparator<User> comparator=new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                User u1=(User)o1;
                User u2=(User)o2;
                return u1.age-u2.age;
            }
        };
        //Collections.sort(list);

        list.forEach(System.out::println);
    }
    @Data
    class User implements  Serializable {
        private static final long serialVersionUID = 4575083234166325541L;
        private String name;
        private Integer age;
        public User() {
        }
        public User(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString(){
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }


        public int compareTo(Object o) {
            User u=(User)o;
            return age-u.age;
        }
    }

    @SneakyThrows
    @Test
    public void test7(){
        String path="D:\\1.log";
        ObjectOutputStream outStream=new ObjectOutputStream(new FileOutputStream(path));
        ObjectInputStream inStream=new ObjectInputStream(new FileInputStream(path));
        User u=new User("abc",123);
        outStream.writeObject(u);
        Object o = inStream.readObject();
        System.out.println(o.toString());

    }
}
